<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 获取表模型
$table_er_kami_type = C::t('#erling_kami_buy#er_kami_buy_type');

if($_GET['action_kami_buy']){//处理增删改
    if($_GET['formhash'] != FORMHASH){
        exit('error');
    }
    if($_GET['action_kami_buy'] == 'delete'){
        $table_er_kami = C::t('#erling_kami_buy#er_kami_buy');
        try {
            DB::query('START TRANSACTION');
            $table_er_kami_type->delete($_GET['action_kami_buy_id']);
            $table_er_kami->delete_by_kami_type($_GET['action_kami_buy_id']);
            DB::query('COMMIT');
        } catch (Exception $e) {
            DB::query('ROLLBACK');
            cpmsg('操作失败: '.$e->getMessage(), '', 'error');
            exit;
        }
    }elseif($_GET['action_kami_buy'] == 'edit'){
        $data = array(
            'name' => $_POST['name'],
            'extcredits' => intval($_POST['extcredits']),
            'credit' => intval($_POST['credit']),
            'sort' => intval($_POST['sort']),
            'desc' => $_POST['desc'],
        );
        $table_er_kami_type->update($_GET['action_kami_buy_id'], $data);
    }elseif($_GET['action_kami_buy'] == 'add'){
        $data = array(
            'name' => $_POST['name'],
            'extcredits' => intval($_POST['extcredits']),
            'credit' => intval($_POST['credit']),
            'sort' => intval($_POST['sort']),
            'desc' => $_POST['desc'],
            'create_time' => TIMESTAMP,
            'update_time' => TIMESTAMP
        );
        $table_er_kami_type->insert($data);
    }
    // 刷新
    $url_page = ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_buy&pmod=admin_kami_buy_type';
    header('Location: '.$url_page);
    exit;
}

$url_page = ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_buy&pmod=admin_kami_buy_type';

// 获取当前列表
$page = max(1, intval($page));
$pagesize = max($_GET['pagesize']??20, intval($pagesize));
$start = ($page - 1) * $pagesize;

$expire_time = isset($_GET['expire_time']) ? strtotime($_GET['expire_time']) : TIMESTAMP;
$expire_date = ($expire_time != -1 && is_numeric($expire_time)) ? date('Y-m-d', $expire_time) : '';// 转换为日期格式

$query = [];
if(!empty($_GET['id'])){
    $query['id'] = intval($_GET['id']);
    $url_page .= '&id='.$_GET['id'];
}
if(!empty($_GET['name'])){
    $query['name'] = intval($_GET['name']);
    $url_page .= '&name='.$_GET['name'];
}
if(!empty($_GET['extcredits'])){
    $query['extcredits'] = intval($_GET['extcredits']);
    $url_page .= '&extcredits='.$_GET['extcredits'];
}
if(!empty($_GET['credit'])){
    $query['credit'] = intval($_GET['credit']);
    $url_page .= '&credit='.$_GET['credit'];
}
$result = $table_er_kami_type->fetch_all($page, $pagesize, $query, $expire_time);

$kami_list = $result['data'];
$total = $result['total'];

if(!empty($_GET['pagesize'])){
    $url_page .= '&pagesize='.intval($_GET['pagesize']);
}
$multipage = multi($total, $pagesize, $page, $url_page);

// 积分类型
$extcredits_array = DB::fetch_all("SELECT * FROM %t WHERE skey LIKE %s", array('common_setting', 'extcredits%'));
if ($extcredits_array && isset($extcredits_array[0]['svalue'])) {
    $extcredits_data = unserialize($extcredits_array[0]['svalue']);
} else {
    $extcredits_data = [];
}

include template('erling_kami_buy:admin_kami_buy_type');
